<?php

/**
 * Framework_Session
 *
 * @author Application Support Group
 * @copyright University of Georgia
 * @package BAMF
 * @filesource
 */

/**
 * Framework_Session
 *
 * Our base session class as a singleton. Handles creating the session, 
 * writing to the session variable (via overloading) and destroying the
 * session.
 *
 * @author Application Support Group
 * @package BAMF
 */
class Framework_Session {
	/**
	 * $instance
	 *
	 * Instance variable used for singleton pattern. Stores a single instance
	 * of Framework_Session.
	 *
	 * @author Application Support Group
	 * @var mixed $instance
	 */
	private static $instance;

	/**
	 * $sessionID
	 *
	 * The session ID assigned by PHP (usually a 32 character alpha-numeric
	 * string).
	 *
	 * @author Application Support Group
	 * @var string $sessionID
	 */
	public static $sessionID;

	/**
	 * __construct
	 *
	 * Starts the session and sets the sessionID for the class.
	 *
	 * @author Application Support Group
	 * @return void
	 */
	private function __construct() {
		session_name( FRAMEWORK_SESSION_NAME );
		session_start();

		if (isset($_COOKIE[session_name()]) && 
				defined('FRAMEWORK_SESSION_COOKIE_TIMEOUT') && 
				FRAMEWORK_SESSION_COOKIE_TIMEOUT > 0) {

			setcookie(session_name(), $_COOKIE[session_name()], 
					time() + FRAMEWORK_SESSION_COOKIE_TIMEOUT, "/");

		}
		self::$sessionID = session_id();
	}

	/**
	 * singleton
	 * 
	 * Implementation of the singleton pattern. Returns a single instance
	 * of the session class.
	 * 
	 * @author Application Support Group
	 * @return mixed Instance of session
	 */
	public static function singleton() {
		if (!isset(self::$instance)) {
			$className = __CLASS__;
			self::$instance = new $className;
		}

		return self::$instance;
	}

	/**
	 * destroy
	 *
	 * Destroys the session
	 *
	 * @author Application Support Group
	 * @access public
	 * @return void
	 */
	public function destroy() {
		$_SESSION = array() ;

		if (isset($_COOKIE[session_name()])) {
			setcookie(session_name(), '', time()-42000, '/');
		}

		session_destroy();
	}

	/**
	 * __clone
	 *
	 * Disable PHP5's cloning method for session so people can't make copies
	 * of the session instance.
	 * 
	 * @author Application Support Group
	 * @access public
	 * @return void
	 */
	public function __clone() {
		trigger_error('Clone is not allowed for '.__CLASS__,E_USER_ERROR);
	}

	/**
	 * __get($var)
	 *
	 * Returns the requested session variable, or false if it does not exist.
	 *
	 * @author Application Support Group
	 * @return mixed
	 * @see Framework_Session::__get()
	 */
	public function __get($var) {
		return !empty( $_SESSION[$var] ) ? $_SESSION[$var] : false ;
	}

	/**
	 * __set 
	 * 
	 * Using PHP5's overloading for setting and getting variables we can
	 * use $session->var = $val and have it stored in the $_SESSION 
	 * variable. To set an email address, for instance you would do the
	 * following:
	 *
	 * <code>
	 * $session->email = 'user@example.com';
	 * </code>
	 *
	 * This doesn't actually store 'user@example.com' into $session->email,
	 * rather it is stored in $_SESSION['email'].
	 * 
	 * @author Application Support Group
	 * @param string $var
	 * @param mixed $val
	 * @see Framework_Session::__get()
	 * @link http://us3.php.net/manual/en/language.oop5.overloading.php
	 */
	public function __set( $var, $val ) {
		return( $_SESSION[$var] = $val );
	}

	/**
	 * __destruct()
	 *
	 * Writes the current session.
	 *
	 * @author Application Support Group
	 */
	public function __destruct() {
		session_write_close();
	}
}

?>
